package com.binance.healthcenter.monitor.health;

import com.binance.healthcenter.model.HealthCheckResult;
import com.binance.healthcenter.notification.NotificationStrategyService;
import com.binance.healthcenter.notification.SmsNotificationService;
import com.binance.healthcenter.notification.TelegramNotificationService;
import com.binance.healthcenter.service.CAdvisorService;
import com.binance.healthcenter.service.HealthCheckService;
import com.binance.master.utils.JsonUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Log4j2
@Service
public class HealthMonitorService {
    @Autowired
    private HealthCheckService healthCheckService;
    @Autowired
    private TelegramNotificationService telegramNotificationService;
    @Autowired
    private SmsNotificationService smsNotificationService;
    @Autowired
    private NotificationStrategyService notificationStrategyService;
    @Autowired
    private CAdvisorService cAdvisorService;

    public void monitorUnHealthService() {
        List<HealthCheckResult> healthCheckResultList = healthCheckService.getAllUnHealthInfo();
        if (CollectionUtils.isEmpty(healthCheckResultList)) {
            return;
        }
        List<HealthCheckResult> pushHealthResults= notificationStrategyService.needNotification(healthCheckResultList);
        log.error("HealthMonitorService.monitorUnHealthService pushUnhealthInfo={}",
                JsonUtils.toJsonNotNullKey(pushHealthResults));
        if(CollectionUtils.isNotEmpty(pushHealthResults)){
            telegramNotificationService.sendTextMessage(pushHealthResults);
            smsNotificationService.sendSmsForhealthCheckResultList(pushHealthResults);
        }
    }

    public void monitorCAdvisorServiceUnHealthService() {
        List<HealthCheckResult> healthCheckResultList = cAdvisorService.getAllUnHealthInfo();
        if (CollectionUtils.isEmpty(healthCheckResultList)) {
            return;
        }
        log.info("HealthMonitorService.monitorCAdvisorServiceUnHealthService unhealthInfo={}",
                JsonUtils.toJsonNotNullKey(healthCheckResultList));
        List<HealthCheckResult> pushHealthResults= notificationStrategyService.needNotification(healthCheckResultList);
        log.error("HealthMonitorService.monitorCAdvisorServiceUnHealthService pushUnhealthInfo={}",
                JsonUtils.toJsonNotNullKey(pushHealthResults));
        if(CollectionUtils.isNotEmpty(pushHealthResults)){
            //telegramNotificationService.sendTextMessage(pushHealthResults);
           // smsNotificationService.sendSmsForhealthCheckResultList(pushHealthResults);
        }
    }

}
